package com.fishery.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.base.entity.BaseSupply;
import com.fishery.base.entity.ConstantValue;
import com.fishery.base.entity.SupplyOut;
import com.fishery.base.entity.vo.BaseSupplyAccessVo;
import com.fishery.base.mapper.BaseSupplyMapper;
import com.fishery.base.mapper.SupplyOutMapper;
import com.fishery.base.service.SupplyOutService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fishery.base.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Ghost
 * @since 2021-01-15
 */
@Service
public class SupplyOutServiceImpl extends ServiceImpl<SupplyOutMapper, SupplyOut> implements SupplyOutService {

	@Autowired
	private SupplyOutMapper supplyOutMapper;

	@Autowired
	private BaseSupplyMapper baseSupplyMapper;

	@Override
	@Transactional
	public boolean updateSupplyOutRecords(SupplyOut supplyOut) {
		// 查询原先的出库记录
		SupplyOut oldSupplyOut = supplyOutMapper.selectById(supplyOut.getId());
		if (oldSupplyOut == null) {
			return false;
		}
		// 判断修改是否修改了出库的重量，如果是，则需要修改基地的该中投入品的总库存重量
		if (supplyOut.getOutWeight() != null
				&& supplyOut.getOutWeight().compareTo(ConstantValue.ZERO) > 0
				&& !supplyOut.getOutWeight().equals(oldSupplyOut.getOutWeight())) {
			// 修改了重量,计算出相差值
			BigDecimal delt = oldSupplyOut.getOutWeight().subtract(supplyOut.getOutWeight());
			// 查询基地中该投入品的情况
			QueryWrapper wrapper = new QueryWrapper();
			wrapper.eq("supply_id", oldSupplyOut.getSupplyId());
			wrapper.eq("base_id", oldSupplyOut.getBaseId());
			BaseSupply baseSupply = baseSupplyMapper.selectOne(wrapper);
			// 修改该基地中此投入品的重量
			baseSupply.setSurplusWeight(baseSupply.getSurplusWeight().add(delt));
			baseSupply.setTotalWeight(baseSupply.getTotalWeight().add(delt));
			// 更新投入品总库存该投入品的总重量到数据库中
			baseSupplyMapper.updateById(baseSupply);
		}
		// 更新出库记录到数据库中
		supplyOutMapper.updateById(supplyOut);
		return true;
	}

	@Override
	public Page<SupplyOut> pageSupplyOutRecords(String baseId, String supplyId, int size, int current) {
		if (size <= 0) {
			size = 5;
		}
		if (current <= 0) {
			current = 1;
		}
		Page<SupplyOut> page = new Page<>(current, size);
		QueryWrapper<SupplyOut> wrapper = new QueryWrapper();
		wrapper.eq("base_id", baseId);
		wrapper.eq("supply_id", supplyId)
				.orderByDesc("gmt_create");
		supplyOutMapper.selectPage(page, wrapper);
		return page;
	}

	@Override
	public Page<SupplyOut> pageSupplyOutRecords(String baseId, int size, int current) {
		if (size <= 0) {
			size = 5;
		}
		if (current <= 0) {
			current = 1;
		}
		Page<SupplyOut> page = new Page<>(current, size);
		QueryWrapper<SupplyOut>  wrapper = new QueryWrapper();
		wrapper.eq("base_id", baseId)
				.orderByDesc("gmt_create");
		supplyOutMapper.selectPage(page, wrapper);
		return page;
	}

	@Override
	@Transactional
	public boolean createSupplyOutRecords(SupplyOut supplyOut) {
		// 从基地投入品总库中获取投入品
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("supply_id", supplyOut.getSupplyId());
		wrapper.eq("base_id", supplyOut.getBaseId());
		BaseSupply baseSupply = baseSupplyMapper.selectOne(wrapper);
		if (baseSupply == null || baseSupply.getSurplusWeight().compareTo(supplyOut.getOutWeight()) < 0) {
			// 不存在该投入品，或投入品数量不足
			return false;
		}
		// 进行出库操作，并记录出库信息
		supplyOutMapper.insert(supplyOut);
		// 对基地的投入品进行出库
		baseSupply.setSurplusWeight(baseSupply.getSurplusWeight().subtract(supplyOut.getOutWeight()));
		baseSupplyMapper.updateById(baseSupply);
		return true;
	}

	@Override
	public void getSupplyOutRecordsByExcel(HttpServletResponse response, String baseId) {
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("base_id", baseId);
		ExcelUtil.write(response, "基地投入品出库记录", SupplyOut.class, "投入品出库记录", supplyOutMapper.selectList(wrapper));
	}

	@Override
	public List<BaseSupplyAccessVo> getAmountInfoOfBaseSupplyOut(String baseId) {
		return supplyOutMapper.findAmountInfoOfBaseSupplyOut(baseId);
	}
}
